
//  GXSoundManager.swift
//  BallGame
//
//  Created by GuangXiao on 16/3/2.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import AVFoundation


 var dict:NSMutableDictionary = NSMutableDictionary()

class GXSoundManager: NSObject {
    
//    var dict:NSMutableDictionary?
//
//    override init() {
//        super.init()
//        
//        dict = NSMutableDictionary()
//    }
    
//     override class func load()
//    {
//        print("diaoyongle load")
//    }
    //播放音效
    func playSoundWithFileName(fileName:String){
        
        var soundID = (dict.objectForKey(fileName) ?? 0).unsignedIntValue as SystemSoundID
        
        if (soundID == 0){
            //获取声音地址
            let path = NSBundle.mainBundle().pathForResource(fileName, ofType: nil)
            //地址转换
            let baseURL = NSURL(fileURLWithPath: path!)
            //赋值
            AudioServicesCreateSystemSoundID(baseURL, &soundID)
            
            dict.setValue("\(soundID)", forKey: fileName)
//            dict.setValue(<#T##value: AnyObject?##AnyObject?#>, forKey: <#T##String#>)
        }
        //播放声音
        AudioServicesPlaySystemSound(soundID)
        print(soundID)
    }
    
    /**
     销毁音效的soundID
     
     - parameter fileName: <#fileName description#>
     */
    func disposeMusicWithFileName(fileName:String){
        
        let soundID = (dict.objectForKey(fileName) ?? 0).unsignedIntValue as SystemSoundID
        
        if (soundID != 0){
            
            AudioServicesDisposeSystemSoundID(soundID)
            
            dict.removeObjectForKey(fileName)
        }
    }
    
    

}
